import { Injectable, ElementRef, Injector, ApplicationRef, ComponentFactoryResolver, ComponentRef, EmbeddedViewRef } from '@angular/core';
import { CheesePopupTipComponent } from './popup-tip.component';
import { PopupTipConfig, PopupTipType } from './popup-tip.model';

@Injectable({
  providedIn: 'root'
})
export class CheesePopupTipService {
  private tipComponentRef: ComponentRef<CheesePopupTipComponent> | null = null;

  constructor(
    private componentFactoryResolver: ComponentFactoryResolver,
    private appRef: ApplicationRef,
    private injector: Injector
  ) {}

  /**
   * 在指定元素附近显示提示
   * @param targetElement 目标元素
   * @param message 提示消息
   * @param config 配置选项
   */
  show(targetElement: ElementRef | HTMLElement, message: string, config?: Partial<PopupTipConfig>): void {
    // 如果已经有提示在显示，先移除它
    this.hide();

    // 创建提示组件
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(CheesePopupTipComponent);
    this.tipComponentRef = componentFactory.create(this.injector);

    // 设置配置
    const tipConfig: PopupTipConfig = {
      message: message,
      type: config?.type || 'info',
      duration: config?.duration || 2000,
      closable: config?.closable !== undefined ? config.closable : false,
      position: config?.position || 'top'
    };

    this.tipComponentRef.instance.config = tipConfig;
    this.tipComponentRef.instance.targetElement = targetElement instanceof ElementRef 
      ? targetElement 
      : { nativeElement: targetElement };

    // 附加到应用
    this.appRef.attachView(this.tipComponentRef.hostView);

    // 获取DOM元素
    const domElem = (this.tipComponentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement;

    // 添加到body
    document.body.appendChild(domElem);

    // 显示提示
    this.tipComponentRef.instance.show();
  }

  /**
   * 显示成功提示
   */
  success(targetElement: ElementRef | HTMLElement, message: string, config?: Partial<PopupTipConfig>): void {
    this.show(targetElement, message, { ...config, type: 'success' });
  }

  /**
   * 显示错误提示
   */
  error(targetElement: ElementRef | HTMLElement, message: string, config?: Partial<PopupTipConfig>): void {
    this.show(targetElement, message, { ...config, type: 'error' });
  }

  /**
   * 显示信息提示
   */
  info(targetElement: ElementRef | HTMLElement, message: string, config?: Partial<PopupTipConfig>): void {
    this.show(targetElement, message, { ...config, type: 'info' });
  }

  /**
   * 显示警告提示
   */
  warning(targetElement: ElementRef | HTMLElement, message: string, config?: Partial<PopupTipConfig>): void {
    this.show(targetElement, message, { ...config, type: 'warning' });
  }

  /**
   * 隐藏当前显示的提示
   */
  hide(): void {
    if (this.tipComponentRef) {
      this.tipComponentRef.instance.hide();
      
      // 设置一个短暂的延迟，等待隐藏动画完成后再销毁组件
      setTimeout(() => {
        if (this.tipComponentRef) {
          this.appRef.detachView(this.tipComponentRef.hostView);
          this.tipComponentRef.destroy();
          this.tipComponentRef = null;
        }
      }, 300);
    }
  }
} 